package com.softer.chengxin.common.domin;

import com.fasterxml.jackson.annotation.JsonGetter;
import com.softer.chengxin.common.util.ParamUtils;
import org.apache.commons.lang3.StringUtils;

import java.text.DecimalFormat;
import java.util.Collections;
import java.util.List;

/**
 * 分页实体
 *
 * @param <T>
 * @author Gong Zhiyang
 */
public class Page<T> implements java.io.Serializable {
    private static final long serialVersionUID = -9052775243341374898L;
    /**
     * 当前页(bootStrap)
     */
    private Integer page = 1;
    /**
     * 最大页/总页数
     */
    private Integer maxPage;

    /**
     * 每页长度
     */
    private Integer limit = 10;
    /**
     * 总记录
     */
    private Integer total = 0;
    /**
     * 查询条件
     */
    private T queryBean;
    /**
     * 结果集
     */
    private List resultList;


    public Page() {
    }

    @JsonGetter("rows")
    public List getResultList() {
        return resultList;
    }

    public void setResultList(List resultList) {
        this.resultList = resultList;
    }

    public Integer getMaxPage() {
        return maxPage;
    }

    public void setMaxPage(Integer maxPage) {
        this.maxPage = maxPage;

    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public T getQueryBean() {
        return queryBean;
    }

    public void setQueryBean(T queryBean) {
        this.queryBean = queryBean;
    }

    public Integer getTotal() {
        return total;
    }

    public void setTotal(Integer total) {
        this.total = total;
        this.maxPage = Integer.parseInt(new DecimalFormat("#0").format(Math
                .ceil(total / 1.0 / limit)));
    }

    /**
     * 获取当前分页开始坐标
     *
     * @return
     */
    public Integer getBeginIndex() {
        return ((this.page - 1) * this.limit);
    }

    public static void getEmptyResult(Page page, MessageBean messageBean){
        if(page==null||messageBean==null) {
            return;
        }
        page.setTotal(0);
        page.setResultList(Collections.EMPTY_LIST);
        page.setQueryBean(messageBean);
    }

    /**
     * 一般用于页面查询返回通用错误信息
     * @param page   分页查询组件
     * @param e          异常信息
     * @return
     */
    public static Page getCommonErrorMsg(Page page,Exception e){
        if(!ParamUtils.allNotNull(page,e)) {
            return page;
        }
        getEmptyResult(page, MessageBean.fail(StringUtils.defaultIfBlank(e.getLocalizedMessage(),"查询出错")));
        return page;
    }


    @Override
    public String toString() {
        final StringBuffer sb = new StringBuffer("Page{");
        sb.append("page=").append(page);
        sb.append(", maxPage=").append(maxPage);
        sb.append(", limit=").append(limit);
        sb.append(", total=").append(total);
        sb.append(", queryBean=").append(queryBean);
        sb.append(", resultList=").append(resultList);
        sb.append('}');
        return sb.toString();
    }
}
